﻿<template>
    <full-screen-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1000"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="供应商名称" prop="supplierName">
                        <el-input v-model="entityObj.supplierName" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="供应商全称" prop="supplierFullName">
                        <el-input v-model="entityObj.supplierFullName" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <!-- <el-col :span="12">
                    <el-form-item label-width="120px" label="所属机构" prop="orgName">
                        <el-input v-model="entityObj.orgName" disabled />
                    </el-form-item>
                </el-col>-->
                <el-col :span="12">
                    <el-form-item label-width="120px" label="供应商类型" prop="supplierType">
                        <el-radio-group v-model="entityObj.supplierType" class="ml-4">
                            <el-radio :label="0">集采中心</el-radio>
                            <el-radio :label="1">外部供应商</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系人" prop="realName">
                        <el-input v-model="entityObj.realName" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="手机号" prop="mobile">
                        <el-input v-model="entityObj.mobile" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系地址" prop="address">
                        <el-input v-model="entityObj.address" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="品牌">
                        <el-select
                            placeholder="---请选择品牌---"
                            v-model="entityObj.brandCodes"
                            multiple="true"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in optionsBrand"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="状态">
                        <el-switch
                            v-model="entityObj.status"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="是否本地供应商">
                        <el-switch
                            v-model="entityObj.isLocal"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="备注">
                        <el-input v-model="entityObj.remarks" type="textarea" :rows="2" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>

        <!-- 供应商 -->
        <template>
            <el-dialog
                v-model="supplierState"
                title="存在相应的供应商，是否与要添加的匹配。"
                :center="true"
                :width="1000"
                append-to-body
            >
                <el-row style="margin-bottom: 10px;">
                    <match-supplier-dialog ref="scMatchSupplierChoice" :data="entityObj.suppliers"></match-supplier-dialog>
                </el-row>
                <template #footer>
                    <el-button @click="supplierState = false">取 消</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="saveSupplier">否</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="setSupplierChoice">是</el-button>
                </template>
            </el-dialog>
        </template>
    </full-screen-dialog>
</template>

<script>
import tool from "@/utils/tool";
import matchSupplierDialog from "./matchSupplier";
export default {
    components: {
        matchSupplierDialog,
    },
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                status: 1,
            },
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            supplierState: false,
            optionsBrand: [],
            //验证规则
            rules: {
                supplierType: [
                    { required: true, type: "number", message: "请填写" },
                ],
                supplierName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                supplierFullName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                realName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                mobile: [{ required: true, type: "string", message: "请填写" }],
                address: [
                    { required: true, type: "string", message: "请填写" },
                ],
            },
        };
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    if (this.mode == "Add") {
                        var verifyValue =
                            await this.$API.auth.PostProcessApi.post(
                                "supplier/pre-verify",
                                { ...this.entityObj }
                            );
                        if (verifyValue.status == 200) {
                            var list = verifyValue.response;
                            this.entityObj.preGuid = verifyValue.msg;
                            this.entityObj.suppliers = list; 
                            console.log('suppliers',this.entityObj.suppliers)
                            if (list && list?.length > 0) {
                                this.supplierState = true;
                                this.isSaveing = false;
                                return;
                            }
                        }
                    }
                    this.saveSupplier();
                }
            });
        },
        //保存供应商
        async saveSupplier() {
            this.supplierState = false;
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    this.mode == "Add" ? "supplier/create" : "supplier/update",
                    { ...this.entityObj }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$emit("fatherMethod");
                    this.isSaveing = false;
                    this.visible = false;
                } else {
                    this.isSaveing = false;
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.isSaveing = false;
                this.loading = false;
                return false;
            }
        },
        //选择供应商进行绑定
        async setSupplierChoice() {
            let suppliers = this.$refs.scMatchSupplierChoice.selectionRows;
            if (!suppliers || suppliers.length == 0) {
                this.$message.error("请选择一个匹配供应商");
                return;
            }

            if (!suppliers || suppliers.length > 1) {
                this.$message.error("请选择一个匹配供应商");
                return;
            }
            this.isSaveing = true;
            this.entityObj.supplierIds = suppliers.map((p) => p.supplierId);
            //绑定
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "purchase-unity-supplier-relation/set-relations",
                    { ...this.entityObj }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$emit("fatherMethod");
                    this.isSaveing = false;
                    this.visible = false;
                } else {
                    this.isSaveing = false;
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.isSaveing = false;
                this.loading = false;
                this.supplierState = false;
                return false;
            }
        },
        //表单注入数据
        async setData(data) {
            this.entityObj = { ...data };
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/dic-options",
                    { ParentBaseDictionaryCodes: ["001"] }
                );
                if (returnValue.status == 200) {
                    this.optionsBrand = returnValue.response.find(
                        (p) => p.code == "001"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        this.entityObj.orgName = tool.data.get("USER_INFO").OrgName;
        await this.initOptions();
    },
};
</script>

<style></style>
